import { getCampusSelector } from '@/api/campus/campus'
import { getGoodsSelectorDetails } from '@/api/services/goods'
import { getDictionaryDataSelector } from '@/api/systemData/dictionary'
import request from '@/utils/request'
export default {
  components: {},
  props: [],
  data() {
    return {
      loading: false,
      visible: false,
      isDetail: false,
      dataForm: {
        id: '',
        brand: 'bell',
        campusId: undefined,
        orderNo: undefined,
        orderTitle: undefined,
        orderSource: undefined,
        orderType: undefined,
        payStatus: undefined,
        payNo: undefined,
        payTime: undefined,
        payment: undefined,
        orderItems: undefined
      },
      campusOptions: [],
      brandOptions: [],
      rules: {
        campusId: [
          {
            required: true,
            message: '请选择校区',
            trigger: 'change'
          }
        ],
        orderNo: [
          {
            required: true,
            message: '请输入外部订单编号',
            trigger: 'blur'
          }
        ],
        orderTitle: [
          {
            required: true,
            message: '请输入订单名称',
            trigger: 'blur'
          }
        ],
        orderSource: [
          {
            required: true,
            message: '请选择订单来源',
            trigger: 'change'
          }
        ],
        orderType: [
          {
            required: true,
            message: '请选择订单类型',
            trigger: 'change'
          }
        ],
        payStatus: [
          {
            required: true,
            message: '请选择支付状态',
            trigger: 'change'
          }
        ],
        payNo: [
          {
            required: true,
            message: '请输入支付编号',
            trigger: 'blur'
          }
        ],
        payTime: [
          {
            required: true,
            message: '请选择支付时间',
            trigger: 'change'
          }
        ],
        payment: [
          {
            required: true,
            message: '请输入支付金额',
            trigger: 'blur'
          }
        ]
      },
      typeOptions: [],
      // 1-家校购买2-系统生成
      orderSourceOptions: [
        {
          enCode: '1',
          fullName: '家校购买'
        },
        {
          enCode: '2',
          fullName: '系统生成'
        }
      ],
      // 1-赠送2-购买
      orderTypeOptions: [
        {
          enCode: '1',
          fullName: '赠送'
        },
        {
          enCode: '2',
          fullName: '购买'
        }
      ],
      payStatusOptions: [
        {
          enCode: '1',
          fullName: '已支付'
        },
        {
          enCode: '2',
          fullName: '未支付'
        }
      ],
      goodsOptions: [],
      goodsTypeOptions: []
    }
  },
  computed: {},
  watch: {
    'dataForm.brand': function(val) {
      this.getGoodsOptions(val)
    }
  },
  created() {
    this.getTypeOptions()
  },
  mounted() {
    this.getGoodsOptions(this.dataForm.brand)

    getDictionaryDataSelector('Brand').then(res => {
      this.brandOptions = res.data.list
    })
    this.handleChangeBrand('bell')
  },
  methods: {
    getGoodsOptions(brand) {
      getGoodsSelectorDetails(brand).then(res => {
        this.goodsOptions = res.data
      })
    },
    handleDelGiveList(index) {
      this.dataForm.orderItems.splice(index, 1)
    },
    addHandleGiveList() {
      let item = {
        campusId: undefined,
        description: undefined,
        goodsCategory: undefined,
        goodsCategoryName: undefined,
        goodsCode: undefined,
        goodsId: undefined,
        goodsName: undefined,
        goodsCategory: undefined,
        goodsCategoryName: undefined,
        goodsType: undefined,
        goodsTypeName: undefined,
        price: undefined,
        goodsNum: 0,
        sendLogistics: 0,
        description: undefined
      }
      this.dataForm.orderItems = this.dataForm.orderItems || []
      this.dataForm.orderItems.push(item)
    },
    getGoodsTypeOptions(scope) {
      console.log(scope)
      /*scope.row.standards = undefined
      this.goodsTypeOptions = []
      getGoodsType(scope.row.orderItems).then(res => {
        this.goodsTypeOptions = res.data
      })*/
    },
    handleChangeBrand(val) {
      getCampusSelector({ brand: val, fields: 'id,fullName' }).then(res => {
        this.campusOptions = Array.isArray(res.data) ? res.data : []
        this.dataForm.campusId = undefined
      })
    },
    getTypeOptions() {
      this.$store.dispatch('base/getDictionaryData', { sort: 'GoodsType' }).then(res => {
        this.typeOptions = this.isDetail ? res : res.filter(o => o.enabledMark == 1)
      })
    },
    goBack() {
      this.$emit('refresh')
    },
    init(id, isDetail) {
      this.dataForm.id = id || 0
      this.visible = true
      // this.dataForm.upTime = +new Date()
      this.isDetail = isDetail || false
      this.$nextTick(() => {
        this.$refs['elForm'].resetFields()
        if (this.dataForm.id) {
          request({
            url: `/api/services/goods/${this.dataForm.id}`,
            method: 'get'
          }).then(res => {
            this.dataForm = res.data
          })
        }
      })
    },
    dataFormSubmit() {
      this.$refs['elForm'].validate(valid => {
        if (valid) {
          if (!this.dataForm.id) {
            request({
              url: `/api/campus/order`,
              method: 'post',
              data: this.dataForm
            }).then(res => {
              this.$message({
                message: res.msg,
                type: 'success',
                duration: 1000,
                onClose: () => {
                  ;(this.visible = false), this.$emit('refresh', true)
                }
              })
            })
          } else {
            request({
              url: `/api/services/goods/${this.dataForm.id}`,
              method: 'PUT',
              data: this.dataForm
            }).then(res => {
              this.$message({
                message: res.msg,
                type: 'success',
                duration: 1000,
                onClose: () => {
                  this.visible = false
                  this.$emit('refresh', true)
                }
              })
            })
          }
        }
      })
    }
  }
}
